package xyz.lwm.lazycat.web.util;

import xyz.lwm.lazycat.utility.It;
import xyz.lwm.lazycat.web.handle.Context;
import xyz.lwm.lazycat.web.handle.HttpRequest;
import xyz.lwm.lazycat.web.handle.HttpResponse;

/**
 * <h3>http context 工具类</h3>
 *
 * @author lwm
 */
public abstract class ContextUtil {

    /**
     * <h5>获取 http context</h5>
     *
     * @return http context
     */
    public static It<Context> get() {
        return It.of(ContextHolder.get());
    }

    /**
     * <h5>获取 http request</h5>
     *
     * @return http request
     */
    public static It<HttpRequest> getRequest() {
        return get().let(Context::req);
    }

    /**
     * <h5>获取 http response</h5>
     *
     * @return http response
     */
    public static It<HttpResponse> getResponse() {
        return get().let(Context::res);
    }

    /**
     * <h5>获取 http request header</h5>
     *
     * @param headerKey header key
     * @return header value
     */
    public static It<String> getHeader(String headerKey) {
        return getRequest().let(req -> req.getHeader(headerKey));
    }

}
